﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace XiTongJiCheng
{
    public partial class Form1 : Form
    {
        public DataOmega data = new DataOmega();


        string basePath = Path.GetDirectoryName(typeof(Program).Assembly.Location);//获取应用程序所在目录（绝对，不受工作目录影响，建议采用此方法获取路径）
        JsonFileHelper jsonFileHelper = null; //通信协议配置文件

        public Form1()
        {
            InitializeComponent();
            jsonFileHelper = new JsonFileHelper($"{basePath}\\bbbconfig.json"); //通信协议配置文件
        }

        private void Button_Input_Click(object sender, EventArgs e)
        {
            ClickToTakeRecord(textBox_Words.Text, "输入");
        }

        private void Button_Output_Click(object sender, EventArgs e)
        {
            ClickToTakeRecord(textBox_Words.Text, "输出");
        }


        private void 展示ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(TongJi());
            if (form_ShowTongJi == null)
            {
                form_ShowTongJi = new Form_ShowTongJi();
                form_ShowTongJi.CelarSelf += Form_ShowTongJi_CelarSelf;
            }

            form_ShowTongJi.BindData(TongJi());
            form_ShowTongJi.Show();
        }

        

        private void 清空ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            DialogResult r = MessageBox.Show(this, "清除统计?", "友情提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (r == DialogResult.Yes)
            {
                data.Items.Clear(); 
            }
            TongJi();
        }
        /// <summary>
        /// 写Json
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void WriteJsonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            jsonFileHelper.Write<DataOmega>("DataOmega", data);
        }
        /// <summary>
        /// 读Json
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void ReadJsonToolStripMenuItem_Click(object sender, EventArgs e)
        {
            var data = jsonFileHelper.Read<DataOmega>("DataOmega");
            if (data == null)
            {
                this.data.Items.Clear();
            }
            else
            {
                this.data.Items.Clear();
                for (int i = 0; i < data.Items.Count; i++)
                {
                    this.data.Items.Add(data.Items[i]);
                }
            }
            TongJi();
        }
        private void listShowToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (form_ShowList == null)
            {
                form_ShowList = new Form_ShowList();
                form_ShowList.CelarSelf += this.Form_ShowList_CelarSelf;
            }
            
            form_ShowList.BindData(data);
            form_ShowList.Show();

        }

        Form_ShowList form_ShowList = null;
        Form_ShowTongJi form_ShowTongJi = null;
        private void Form_ShowList_CelarSelf(string s)
        {
            if (form_ShowList != null)
            {
                form_ShowList = null;
            }
           
        }
        private void Form_ShowTongJi_CelarSelf(string s)
        {
            if (form_ShowTongJi != null)
            {
                form_ShowTongJi = null;
            }
        }


        #region Tools
        /// <summary>
        /// 记录数据，进行数据累计
        /// </summary>
        /// <param name="word"></param>
        /// <param name="type"></param>
        private void ClickToTakeRecord(string word, string type)
        {
            data.Items.Add(new Item_For_Statistics(word, type));

            TongJi();
        }
        /// <summary>
        /// 统计数据
        /// </summary>
        /// <returns></returns>
        private string TongJi()
        {
            string result = "";

            int inputCount = 0;
            int outputCount = 0;

            List<Item_For_Statistics> tempList = new();

            for (int i = 0; i < data.Items.Count; i++)
            {
                #region 输入输出数量累计
                if (data.Items[i].Type == "输入")
                {
                    inputCount++;
                }
                else if (data.Items[i].Type == "输出")
                {
                    outputCount++;
                }

                #endregion

                #region 名称不同累计
                int index = CanFind_With_Name(data.Items[i].Name, tempList);
                if (index != -1)
                {
                    
                    switch (data.Items[i].Type)
                    {
                        case "输入":
                            tempList[index].Count_In_Input++;
                            break;
                        case "输出":
                            tempList[index].Count_In_Output++;
                            break;
                        default:
                            break;
                    }
                }
                else
                {
                    switch (data.Items[i].Type)
                    {
                        case "输入":
                            tempList.Add(new Item_For_Statistics(data.Items[i].Name, 1, 0));
                            break;
                        case "输出":
                            tempList.Add(new Item_For_Statistics(data.Items[i].Name, 0, 1));
                            break;
                        default:
                            break;
                    }
                    
                }
                #endregion
            }

            label_InputCount.Text = inputCount.ToString();
            label_OutputCount.Text = outputCount.ToString();


            result = $"总共有\r\n输入：{inputCount}; 输出：{outputCount}";

            
            for (int i = 0; i < tempList.Count; i++)
            {
                int length = tempList[i].Name.Length;
                if (length<3)
                {
                    result = $"{result}\r\n\t【{tempList[i].Name}】\t\t\t总数量：{tempList[i].Count_In_Input + tempList[i].Count_In_Output}\t输入：{tempList[i].Count_In_Input}\t输出：{tempList[i].Count_In_Output}";
                }
                else if (length >= 8)
                {
                    result = $"{result}\r\n\t【{tempList[i].Name}】\t总数量：{tempList[i].Count_In_Input + tempList[i].Count_In_Output}\t输入：{tempList[i].Count_In_Input}\t输出：{tempList[i].Count_In_Output}";
                }
                else
                {
                    result = $"{result}\r\n\t【{tempList[i].Name}】\t\t总数量：{tempList[i].Count_In_Input + tempList[i].Count_In_Output}\t输入：{tempList[i].Count_In_Input}\t输出：{tempList[i].Count_In_Output}";
                }
            }

            return result;
        }

        private int CanFind_With_Name(string name, List<Item_For_Statistics> list)
        {
            int result = -1;
            for (int i = 0; i < list.Count; i++)
            {
                if (name == list[i].Name)
                {
                    result = i;
                    break;
                }
            }

            return result;
        }


        #endregion

        
    }
}
